package cn.yuanqiao.archive.formManage.service.impl;

import cn.yuanqiao.archive.formManage.detail.mapper.YqformManageDetailMapper;
import cn.yuanqiao.archive.formManage.domain.YqformManage;
import cn.yuanqiao.archive.formManage.mapper.YqformManageMapper;
import cn.yuanqiao.archive.formManage.service.IYqformManageService;
import cn.yuanqiao.common.domain.YqTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

/**
 * 表单管理Service业务层处理
 * 
 * @author lyk
 * @date 2023-06-21
 */
@Service
public class YqformManageServiceImpl implements IYqformManageService 
{
    @Autowired
    private YqformManageMapper yqformManageMapper;

    @Autowired
    private YqformManageDetailMapper yqformManageDetailMapper;

    /**
     * 查询表单管理
     *
     * @param fromId 表单管理主键
     * @return 表单管理
     */
    @Override
    public YqformManage selectYqformManageByFromId(String fromId,String tenantCode)
    {
        return yqformManageMapper.selectYqformManageByFromId(fromId,tenantCode);
    }

    /**
     * 查询表单管理列表
     *
     * @param yqformManage 表单管理
     * @return 表单管理
     */
    @Override
    public List<YqformManage> selectYqformManageList(YqformManage yqformManage)
    {
        return yqformManageMapper.selectYqformManageList(yqformManage);
    }

    @Override
    public List<YqformManage> selectYqformManageListOne(YqformManage yqformManage)
    {
        return yqformManageMapper.selectYqformManageListOne(yqformManage);
    }

    /**
     * 新增表单管理
     *
     * @param yqformManage 表单管理
     * @return 结果
     */
    @Override
    public int insertYqformManage(YqformManage yqformManage)
    {
        return yqformManageMapper.insertYqformManage(yqformManage);
    }

    /**
     * 修改表单管理
     *
     * @param yqformManage 表单管理
     * @return 结果
     */
    @Override
    public int updateYqformManage(YqformManage yqformManage)
    {
        return yqformManageMapper.updateYqformManage(yqformManage);
    }

    /**
     * 批量删除表单管理
     *
     * @param fromIds 需要删除的表单管理主键
     * @return 结果
     */
    @Override
    public int deleteYqformManageByFromIds(String[] fromIds,String tenantCode)
    {
        int i = yqformManageMapper.deleteYqformManageByFromIds(fromIds, tenantCode);
        if (i!=0){
            yqformManageDetailMapper.deleteYqformManageDetailByManageIds(fromIds);
        }
        return i;
    }

    /**
     * 删除表单管理信息
     *
     * @param fromId 表单管理主键
     * @return 结果
     */
    @Override
    public int deleteYqformManageByFromId(String fromId,String tenantCode)
    {
        return yqformManageMapper.deleteYqformManageByFromId(fromId,tenantCode);
    }

    @Override
    public int addList(List<YqTable> yqTables,String tenantCode) {
        Iterator<YqTable> iterator = yqTables.iterator();
        while (iterator.hasNext()) {
            YqTable item = iterator.next();
            YqformManage yqformManage = yqformManageMapper.selectYqformManageByFromId(String.valueOf(item.getTableId()),tenantCode);
            if (yqformManage!=null) {
                iterator.remove(); // 删除当前元素
            }
        }
        int i = 0;
        if (yqTables.size()!=0){
            i = yqformManageMapper.addList(yqTables,tenantCode);
        }
        return i;
    }
}